package com.dpxm.zdbdg.charte.sand.service;

import com.alibaba.fastjson.JSONObject;
import com.dpxm.zdbdg.chart.daletou.vo.QueryDaLeTouVO;
import com.dpxm.zdbdg.charte.sand.dao.PaiLieDao;
import com.dpxm.zdbdg.charte.sand.dao.SanDAllDao;
import com.dpxm.zdbdg.charte.sand.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import vehiclebase.enums.QueryEnum;
import vehiclebase.utils.BusinessException;
import vehiclebase.utils.ToolsUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ChartPaiLieService {
    @Autowired
    private PaiLieDao paiLieDao;
    @Autowired
    private SanDAllDao sanDAllDao;


    public String savePaiLie() {
        //排列三官网 对象
        PtQueryDPaiLie3VO ptQueryDaLe = new PtQueryDPaiLie3VO();
        RestTemplate restTemplate = new RestTemplate();
        String daLetTouLiShi = restTemplate.getForObject("https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=35&provinceId=0&pageSize=100&isVerify=1&pageNo=1&termLimits=1", String.class);
        //将官网的数据转化为自有对象
        ptQueryDaLe = JSONObject.parseObject(daLetTouLiShi, PtQueryDPaiLie3VO.class);
        if(ToolsUtils.notEmpty(ptQueryDaLe) && ptQueryDaLe.getErrorMessage().equals(QueryEnum.ERROR_MESSAGE.val())){
            List<Values> valueList = ptQueryDaLe.getValue().getList();
            //将数据存入本地库
            PaiLieVO queryPaiLieVO = new PaiLieVO();
            if(ToolsUtils.notEmpty(valueList)){
                queryPaiLieVO.setNubm(valueList.get(0).getLotteryDrawResult());
                queryPaiLieVO.setCreatedate(valueList.get(0).getLotteryDrawTime());
                queryPaiLieVO.setQihao(valueList.get(0).getLotteryDrawNum());
                queryPaiLieVO.setNubmwu(valueList.get(0).getLotteryUnsortDrawresult());
                //存入数据之前查询是否有当期数据
                PaiLieVO QueryDangQiPaiLie = paiLieDao.QueryDangQi(valueList.get(0).getLotteryDrawNum());
                //没有数据查询
                if(ToolsUtils.isEmpty(QueryDangQiPaiLie)){
                    paiLieDao.insertSelective(queryPaiLieVO);
                }
            }

        }
        return null;

    }

    public SandRes coldHot(QuerySanDReq req) {
        SandRes sandRes = new SandRes();
        if(ToolsUtils.isEmpty(req)){
            throw new BusinessException("参数不能为空" );
        }
        //查询排列三所有的号码 供后续使用 （所有号码和3D的一样）
        List<String> sanDAll= sanDAllDao.QueryMessage();
        //查询最近期数的号码 供后续使用
        List<String> sanDkai = paiLieDao.QueryMessage(req.getQishu());
        //最近的号码需要先去重复
        List<String> cotList=sanDkai.stream().distinct().collect(Collectors.toList());
        // 得到期数内没有开过的号码
        List<String> cotNumbs = sanDAll.stream().filter(a->!cotList.contains(a)).collect(Collectors.toList());
        List<String> list = new ArrayList<>();
        if(ToolsUtils.notEmpty(req.getShaNubm())){  //杀号码
            List<String> shaHaolist = new ArrayList<>();
            for (int j=0 ;j<cotNumbs.size() ; j++){
                if( ToolsUtils.notEmpty(cotNumbs.get(j))){
                    int faleNubm=0;
                    for(int i=0 ;i<req.getShaNubm().size() ; i++){
                        if(!(cotNumbs.get(j).contains(req.getShaNubm().get(i)))){  //表示含有杀号码的号码 需要删除
                            faleNubm=faleNubm+1;
                        }
                    }
                    if(faleNubm == req.getShaNubm().size()){
                        shaHaolist.add(cotNumbs.get(j)); //得到号码
                    }
                }
            }
            list= shaHaolist.stream().distinct().collect(Collectors.toList());
        }
        if(ToolsUtils.isEmpty(list)){  //说明没有杀号过程
            list=cotNumbs;
        };

        List<String> noNumbList=new ArrayList<>(); //无对子的数据集合
        List<String> yesNumbList=new ArrayList<>(); //有对子的数据集合
        List<String> numbfonum=new ArrayList<>();//有某个数的集合
        //if(QueryEnum.FALG_DUIZI_NO.val().equals(req.getFalg())){ //无对子的数据
        for (int i=0 ;i<list.size() ; i++){
            if( ToolsUtils.notEmpty(list.get(i))){
                Boolean st = isUnique(list.get(i));
                if(!st){
                    noNumbList.add(list.get(i)); //无对子的数据
                }else {
                    yesNumbList.add(list.get(i)); //只有对子的数据
                }
            }
        }
        sandRes.setNoNumbList(noNumbList);
        sandRes.setYesNumbList(yesNumbList);
        // }
       /*if(QueryEnum.FALG_DUIZI_YES.val().equals(req.getFalg())){ //只有对子的数据
            for (int j=0 ;j<cotNumb.size() ; j++){
                if( ToolsUtils.notEmpty(cotNumb.get(j))){
                    Boolean st = isUnique(cotNumb.get(j));
                    if(st){
                        yesNumbList.add(cotNumb.get(j));
                    }
                }
            }
       }*/
        if(ToolsUtils.notEmpty(req.getNubm())){ //必开的某个数的集合
            for (int j=0 ;j<list.size() ; j++){
                if( ToolsUtils.notEmpty(list.get(j))){
                    Boolean st =list.get(j).contains(req.getNubm());
                    if(st){
                        numbfonum.add(list.get(j));
                    }
                }
            }
        }
        sandRes.setNumbfonum(numbfonum);
        sandRes.setHotNumber(list);

        return sandRes;
    }
    Map<Character,Character> map=new HashMap<>();
    public boolean isUnique (String string) {
        /**
         * 判断字符串是否含有重复字符：
         *     1.首先将字符串转成字符数组
         *     2.利用 indexof 与 lastindexof 判断字符是否重复
         */
        char[]arr = string.toCharArray();
        for(char ch:arr) {
            if (string.indexOf(ch) != string.lastIndexOf(ch)) {
                // System.out.println("字符串含有重复字符");
                return true;
            }
        }
        return false;
    }
}
